﻿using Nop.Core.Configuration;
using Nop.Core.Domain.Customers;
using Nop.Services.Common;
using System;
using System.Collections.Generic;
using System.Text;
using UserCenter.Console.Sdk;

namespace Nop.Web.Services.Utils
{
    public class HUCSdkHelper
    {
        static Dictionary<string, MemberSdk> sdkList = new Dictionary<string, MemberSdk>();
        private readonly IGenericAttributeService _genericAttributeService;
        private readonly NopConfig _config;

        public HUCSdkHelper(NopConfig config, IGenericAttributeService genericAttributeService)
        {
            this._config = config;
            _genericAttributeService = genericAttributeService;
        }

        public MemberSdk Get()
        {
            return Get(this._config.HUCConnectionString);

        }

        public MemberSdk Get(string connectionString)
        {
            lock (sdkList)
            {
                if (!sdkList.ContainsKey(connectionString))
                {

                    sdkList[connectionString] = CreateSdk(connectionString);
                }
                return sdkList[connectionString];
            }
        }


        private static MemberSdk CreateSdk(string connectionString)
        {
            //signKey@address:port
            if (connectionString.IndexOf("@") > -1 && connectionString.IndexOf(":") > -1 && connectionString.IndexOf(":") > -1)
            {
                string merchant = connectionString.Split('@')[0];
                var merchantId = long.Parse(merchant.Split(':')[0]);
                string signKey = merchant.Split(':')[1];
                string server = connectionString.Split('@')[1];
                string address = server.Split(':')[0];
                var port = int.Parse(server.Split(':')[1]);
                return new MemberSdk(merchantId, signKey, address, port);
            }
            else
            {

                throw new NotSupportedException("sdk连接字符串错误");
            }
        }

        public long GetMemberId(Customer customer)
        {
            long ucMemberId = 0;
            try
            {
                const string prefix = "HUC_Member_";
                const string suffix = "@hsq168.co";

                var email = customer.Email;
                var prefixIndex = email.IndexOf(prefix);
                var suffixIndex = email.IndexOf(suffix);
                if (prefixIndex == 0 && suffixIndex > prefix.Length)
                {
                    var memberIdStr = email.Substring(prefix.Length, suffixIndex - prefix.Length);
                    long.TryParse(memberIdStr, out ucMemberId);
                }
                if (ucMemberId < 1)
                {
                    ucMemberId = _genericAttributeService.GetAttribute<long>(customer, "NOP_UCMemberId");
                }
            }
            catch (Exception ex)
            {
                MX.Core.Log.Logger.Default.Error(ex, "GetMemberId");
            } 
            return ucMemberId;
        }



    }
}
